import {initGL} from './boot/initGL.ts'
import { Timing } from './time/Timing.ts'
import {initProgramGame} from "@/views/cg/lib/boot/initProgramGame.ts";

export default class RenderContext {
  static gl = null
  static programs = {}
  static timing = null

  static init(){
    if(RenderContext.gl){return}
    const gl = initGL()
    gl.canvas.width = gl.canvas.clientWidth;
    gl.canvas.height =gl.canvas.clientHeight;
    RenderContext.gl = gl
    RenderContext.timing = new Timing()
    RenderContext.aspect = gl.canvas.width / gl.canvas.height;
    RenderContext.currentProgram = null
  }

  static initProgram(name='default'){
    const gl = RenderContext.gl
    if(!RenderContext.programs[name]){
      const program = initProgramGame(gl, name);
      RenderContext.programs[name] = program;
    }
  }

  static getAspect(){
    return RenderContext.aspect;
  }

  static getGL(){
    RenderContext.init()
    return RenderContext.gl;
  }

  static getProgram(){
    RenderContext.init()
    if(!RenderContext.currentProgram){
      RenderContext.initProgram('default')
      RenderContext.currentProgram = RenderContext.programs['default']
      RenderContext.gl.useProgram(RenderContext.currentProgram)
    }
    return RenderContext.currentProgram;
  }

  static switchProgram(name){
    RenderContext.init()
    RenderContext.initProgram(name)
    RenderContext.currentProgram = RenderContext.programs[name];
    RenderContext.gl.useProgram(RenderContext.currentProgram);
  }

  static getTiming(){
    RenderContext.init()
    return RenderContext.timing;
  }
}
